package com.googlecode.uipipedream;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.googlecode.uipipedream.impl.ColourPanel;
import com.googlecode.uipipedream.impl.ColourSwatch;
import com.googlecode.uipipedream.impl.HorizontalSliderBar;
import com.googlecode.uipipedream.impl.VerticalSliderBar;
import com.googlecode.uipipedream.spec.ColourModel;

//public class Main extends Applet implements Runnable {
public class Main extends JFrame implements Runnable {
	private static final long serialVersionUID = 2880337302963035000L;
//	private ColourWheel selector;
	private ColourModel model = new ColourModel();
	private ColourPanel colourPanel;
	private ColourSwatch[] swatch = new ColourSwatch[8];
	private JPanel swatchPanel;
	
	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Main());
	}
	
	private boolean swatchDrag = false;

	private ColourSwatch swatchLocate(int x, int y) {
		x -= swatchPanel.getLocation().x;
		y -= swatchPanel.getLocation().y;
		for (int i = 0; i < swatch.length; ++i) {
			if (swatch[i].contains(x, y))
				return swatch[i];
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public boolean mouseDown(Event e, int x, int y) {
		if (colourPanel.getBounds().contains(x, y)) {
			swatchDrag = true;
			oldSwatch = null;
			Container f = this;
			while (f != null && !(f instanceof Frame))
				f = f.getParent();
			if (f != null)
				((Frame) f).setCursor(Frame.CROSSHAIR_CURSOR);
			return true;
		} else
			swatchDrag = false;

		ColourSwatch swatch = swatchLocate(x, y);
		if (swatch != null) {
			model.setColor(swatch.getColor());
			return true;
		}

		return false;
	}

	@SuppressWarnings("deprecation")
	public boolean mouseUp(Event e, int x, int y) {
		if (swatchDrag) {
			Container f = this;
			while (f != null && !(f instanceof Frame))
				f = f.getParent();
			if (f != null)
				((Frame) f).setCursor(Frame.DEFAULT_CURSOR);
			return true;
		}
		return false;
	}

	private ColourSwatch oldSwatch = null;
	private Color oldColor = null;

	public boolean mouseDrag(Event e, int x, int y) {
		if (swatchDrag == false)
			return false;

		ColourSwatch swatch = swatchLocate(x, y);
		if (swatch != oldSwatch) {
			if (oldSwatch != null)
				oldSwatch.setColor(oldColor);
			if (swatch != null) {
				oldColor = swatch.getColor();
				swatch.setColor(model.getColor());
			}
			oldSwatch = swatch;
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		// 1. Setup how this window looks and acts.
		this.setTitle("Assignment Four: A Novel Colour Selector");
		this.setMinimumSize(new Dimension(600,300));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		// 2. Setup components.
		this.getContentPane().setBackground(Color.black);
		this.colourPanel = new ColourPanel(model);
		this.swatch[0] = new ColourSwatch(model, Color.black);
		this.swatch[1] = new ColourSwatch(model, Color.white);
		this.swatch[2] = new ColourSwatch(model, Color.red);
		this.swatch[3] = new ColourSwatch(model, Color.yellow);
		this.swatch[4] = new ColourSwatch(model, Color.green);
		this.swatch[5] = new ColourSwatch(model, Color.cyan);
		this.swatch[6] = new ColourSwatch(model, Color.blue);
		this.swatch[7] = new ColourSwatch(model, Color.magenta);
		this.swatchPanel = new JPanel();
		this.swatchPanel.setLayout(new GridLayout(8, 1));
		this.swatchPanel.setBackground(Color.black);
		this.swatchPanel.setOpaque(true);
		for (int i = 0; i < 8; ++i)
			swatchPanel.add(swatch[i]);
		
		// 3. Add components to the window.
		JPanel sliderPanel = new JPanel();
		sliderPanel.setLayout(new FlowLayout());
		sliderPanel.setBackground(Color.black);
		sliderPanel.add(new VerticalSliderBar(model, VerticalSliderBar.SATURATION));
		sliderPanel.add(new VerticalSliderBar(model, VerticalSliderBar.BRIGHTNESS));
		this.setLayout(new FlowLayout());
		this.add("West", this.swatchPanel);
		this.add("Center", new HorizontalSliderBar(model, HorizontalSliderBar.HUE));
		this.add("East", sliderPanel);
		
		// 4. Set inital colours
		this.model.setBrightness(1f);
		this.model.setSaturation(1f);
		this.model.setColor(Color.red);
		this.validate();
		
		// 5. Display the window and start this application.
		this.setLocation(100,100);
		this.pack();
		this.setVisible(true);
	}
}
